package com.ruoyi.system.service.impl;

import java.util.List;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.account.service.ISxoSafetyLogService;
import com.ruoyi.account.util.HttpResult;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.system.api.IBaseClient;
import com.ruoyi.system.api.impl.UnbindRelateAcctApi;
import com.ruoyi.system.config.PayConfig;
import com.ruoyi.system.config.TaskExecutePoolConfig;
import com.ruoyi.system.util.BusinessException;
import com.ruoyi.system.util.CodeEnum;
import com.ruoyi.system.util.PaPayUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.BankWithdrawalMapper;
import com.ruoyi.system.domain.BankWithdrawal;
import com.ruoyi.system.service.IBankWithdrawalService;

/**
 * 银行提现账户Service业务层处理
 * 
 * @author zhangjf
 * @date 2025-10-13
 */
@Slf4j
@Service
public class BankWithdrawalServiceImpl extends BasePaServiceImpl implements IBankWithdrawalService
{
    @Autowired
    private BankWithdrawalMapper bankWithdrawalMapper;

    @Autowired
    private ISxoSafetyLogService sxoSafetyLogService;
    @Autowired
    private PayConfig payConfig;
    @Autowired
    private TaskExecutePoolConfig taskExecutePoolConfig;

    /**
     * 查询银行提现账户
     * 
     * @param withdrawalAcctId 银行提现账户主键
     * @return 银行提现账户
     */
    @Override
    public BankWithdrawal selectBankWithdrawalByWithdrawalAcctId(Long withdrawalAcctId)
    {
        return bankWithdrawalMapper.selectBankWithdrawalByWithdrawalAcctId(withdrawalAcctId);
    }

    /**
     * 查询银行提现账户列表
     * 
     * @param bankWithdrawal 银行提现账户
     * @return 银行提现账户
     */
    @Override
    public List<BankWithdrawal> selectBankWithdrawalList(BankWithdrawal bankWithdrawal)
    {
        return bankWithdrawalMapper.selectBankWithdrawalList(bankWithdrawal);
    }

    /**
     * 新增银行提现账户
     * 
     * @param bankWithdrawal 银行提现账户
     * @return 结果
     */
    @Override
    public int insertBankWithdrawal(BankWithdrawal bankWithdrawal)
    {
        bankWithdrawal.setCreateTime(DateUtils.getNowDate());
        return bankWithdrawalMapper.insertBankWithdrawal(bankWithdrawal);
    }

    /**
     * 修改银行提现账户
     * 
     * @param bankWithdrawal 银行提现账户
     * @return 结果
     */
    @Override
    public int updateBankWithdrawal(BankWithdrawal bankWithdrawal)
    {
        bankWithdrawal.setUpdateTime(DateUtils.getNowDate());
        return bankWithdrawalMapper.updateBankWithdrawal(bankWithdrawal);
    }

    /**
     * 批量删除银行提现账户
     * 
     * @param withdrawalAcctIds 需要删除的银行提现账户主键
     * @return 结果
     */
    @Override
    public int deleteBankWithdrawalByWithdrawalAcctIds(Long[] withdrawalAcctIds)
    {
        return bankWithdrawalMapper.deleteBankWithdrawalByWithdrawalAcctIds(withdrawalAcctIds);
    }

    /**
     * 删除银行提现账户信息
     * 
     * @param withdrawalAcctId 银行提现账户主键
     * @return 结果
     */
    @Override
    public int deleteBankWithdrawalByWithdrawalAcctId(Long withdrawalAcctId)
    {
        return bankWithdrawalMapper.deleteBankWithdrawalByWithdrawalAcctId(withdrawalAcctId);
    }

    /**
     * 解绑
     *
     * @return
     */
    @Override
    public String unbindRelateAcct(BankWithdrawal bankWithdrawal) {

        JSONObject json = new JSONObject();
        json.put("TxnCode", "KFEJZB6065");
        json.put("FunctionFlag", "1");
        json.put("FundSummaryAcctNo", payConfig.getFundSummaryAcctNo());
        json.put("TranNetMemberCode", bankWithdrawal.getSubAcctno());
        json.put("SubAcctNo", bankWithdrawal.getSubAcctno());
        json.put("MemberAcctNo", "");


        try {
        IBaseClient baseClient = new UnbindRelateAcctApi(sxoSafetyLogService, taskExecutePoolConfig);
        log.info("6065解绑调平安入参："+json);
        log.info("6065解绑调平安入参时间："+ DateUtil.now());
        HttpResult httpResult = baseClient.execute(json);
        if (httpResult.getStatus() != CodeEnum.SUCCESS.getCode()) {
            throw new BusinessException(CodeEnum.SERVER_ERROR.getCode(), httpResult.getError());
        }
        JSONObject extenddata = handleError(httpResult);
        JSONObject obj = buildResult(extenddata);
        log.info("6065解绑调平安回参时间："+DateUtil.now());
        log.info("6065解绑调平安回参："+extenddata);

        return null;

        } catch (Exception e) {
            throw new BusinessException(com.ruoyi.account.enums.CodeEnum.SERVER_ERROR.getCode(), e.getMessage());
        }
    }




}
